<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">权限名称:</label>
                        <div class="layui-input-inline">
                            <input name="authorityName" class="layui-input" placeholder="输入权限名称"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">权限标识:</label>
                        <div class="layui-input-inline">
                            <input name="authority" class="layui-input" placeholder="输入权限标识"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">模块名称:</label>
                        <div class="layui-input-inline">
                            <input name="parentName" class="layui-input" placeholder="输入模块名称"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="authTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>&nbsp;
                        <button id="authSyncBtn" class="layui-btn icon-btn" type="button">
                            <i class="layui-icon">&#xe669;</i>同步
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="authTable" lay-filter="authTable"></table>
        </div>
    </div>
</div>
<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'tableX', 'util', 'admin', 'setter'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var tableX = layui.tableX;
        var util = layui.util;
        var admin = layui.admin;
        var setter = layui.setter;

        /* 渲染表格 */
        var insTb = tableX.render({
            elem: '#authTable',
            url: setter.baseServer + 'sys/authorities/page',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'},
                {field: 'parentName', title: '模块名称', sort: true, align: 'center'},
                {field: 'authorityName', title: '权限名称', sort: true},
                {field: 'authority', title: '权限标识', sort: true},
                {
                    title: '请求方式', templet: function (d) {
                        if (d.authority.indexOf('get:') === 0) {
                            return '<span class="layui-badge layui-badge-blue">GET</span>';
                        } else if (d.authority.indexOf('post:') === 0) {
                            return '<span class="layui-badge layui-badge-green">POST</span>';
                        } else if (d.authority.indexOf('put:') === 0) {
                            return '<span class="layui-badge layui-badge-yellow">PUT</span>';
                        } else if (d.authority.indexOf('delete:') === 0) {
                            return '<span class="layui-badge layui-badge-red">DELETE</span>';
                        }
                    }, align: 'center', width: 100
                },
                {
                    field: 'createTime', title: '同步时间', templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, sort: true, align: 'center'
                }
            ]],
            done: function (res, curr, count) {
                tableX.merges('authTable', [1], ['parentName'], false);
            }
        });

        /* 表格搜索 */
        form.on('submit(authTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 同步 */
        $('#authSyncBtn').click(function () {
            admin.confirm('确定进行同步吗？<br><span style="color: red;">（自动扫描所有接口生成权限标识）</span>', function () {
                var loadIndex = layer.load(2);
                admin.req('sys/authorities/sync', function (res) {
                    layer.close(loadIndex);
                    if (res.code === 0) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'post');
            });
        });

    });
</script>